home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 187_01 / mak_dec.c < prev    next >
C/C++ Source or Header  |  1985-12-29  |  2KB  |  54 lines

  1. /*@*****************************************************/
  2. /*@                                                    */
  3. /*@ mak_dec - insert a decimal point in a numeric      */
  4. /*@        string.  If the string is not long          */
  5. /*@        enough, leading zeros will be inserted.     */
  6. /*@                                                    */
  7. /*@   Usage:     mak_dec(string, places);              */
  8. /*@       where string is a string of digits.          */
  9. /*@             places is the number of places after   */
  10. /*@                the decimal point.                  */
  11. /*@                                                    */
  12. /*@    Returns a pointer to the string.                */
  13. /*@                                                    */
  14. /*@    NOTE:  The string area must be long enough to   */
  15. /*@         hold the original string, the decimal      */
  16. /*@         point, and any added leading zeros.  It    */
  17. /*@         may be wise to copy the string to a large  */
  18. /*@         string buffer before use.                  */
  19. /*@                                                    */
  20. /*@         A typical use might be:                    */
  21. /*@           puts(mak_dec(itoa(n_max, strbuf), 2));   */
  22. /*@                                                    */
  23. /*@*****************************************************/
  24.  
  25. char *mak_dec(s, n)
  26. char *s;
  27. int n;
  28. {
  29.     int len, i;
  30.     char *save;
  31.  
  32.     save = s;
  33.     len = strlen(s);        /* find end of it */
  34.  
  35.     if (len <= n) {
  36.         for (i=len; i >= 0; i--)    /* make room for place holder zeros */
  37.             s[i+n-len+1] = s[i];
  38.         for (i=0; i < n-len+1; i++)
  39.             s[i] = '0';            /* add zero place holders */
  40.         len = n +1;
  41.     }
  42.  
  43.     for (i=len; i >= (len - n); i--) {
  44.         if (s[i] == ' ')
  45.             s[i] = '0';        /* add trailing zeros */
  46.         s[i+1] = s[i];        /* make room for decimal */
  47.     }
  48.  
  49.     s[len-n] = '.';            /* add decimal */
  50.     if (s[len-n-1] == ' ')
  51.         s[len-n-1] = '0';    /* add leading zero before decimal, if necessary */
  52.     
  53.     return save;            /* make avail as stacked function */
  54. }